import {Injectable} from '@angular/core';
import {Observable} from 'rxjs';
import {ConZoneType, ConZoneTypeCondition} from './con-zone-type.type';
import {isFunction, isNullOrUndefined} from 'util';
import {Zero3Alert} from '../../../frame/global/zero3-alert/zero3-alert.component';
import {HttpRequestClient} from '../../../frame/http-request-client/http-request-client';
import {UrlApi, UrlRou} from '../../../frame';
import {ModifyDomain} from '../../../frame/module/modify-domain';

@Injectable()
export class ConZoneTypeService {

    audit = {
        submit: (qm: ConZoneTypeCondition, callback: {success: (result) => void, error: (result) => void}): Observable<any> => {
            return HttpRequestClient.PUT(UrlApi.conZoneType.audit.submit, qm, callback);
        },
        audit: (qm: ConZoneTypeCondition, callback: {success: (result) => void, error: (result) => void}): Observable<any> => {
            return HttpRequestClient.PUT(UrlApi.conZoneType.audit.audit, qm, callback);
        },
        reject: (qm: ConZoneTypeCondition, callback: {success: (result) => void, error: (result) => void}): Observable<any> => {
            return HttpRequestClient.PUT(UrlApi.conZoneType.audit.reject, qm, callback);
        },
    };

    constructor() { }

    delete(qm: ConZoneTypeCondition, callback: {success: (result) => void, error: (result) => void}): Observable<any> {
        return HttpRequestClient.PUT(UrlApi.conZoneType.delete, qm, callback);
    }

    save(domain: ConZoneType, callback: {success: (result) => void, error: (result) => void}): Observable<any> {
        return HttpRequestClient.POST(UrlApi.conZoneType.add, domain, callback);
    }

    list(domain: ConZoneTypeCondition, page: { page: number; size: number; orders: string }): Observable<any> {
        let url = `${UrlApi.conZoneType.list}?`;
        Reflect.ownKeys(ConZoneTypeCondition.prototype).forEach(item => {
            const val = Reflect.get(domain, item);
            if (!isFunction(val) && val && val.length > 0) {
                if (Array.isArray(val)) {
                    val.forEach(it => {
                        url = url.concat(`&${item.toString()}=${it}`);
                    });
                } else {
                    url = url.concat(`&${item.toString()}=${val}`);
                }
            }
        });
        if (url.indexOf('?&') === -1) {
            url = url.concat(`page=${page.page}&size=${page.size}&orders=${page.orders}`);
        } else {
            url = url.replace('?&', '?').concat(`&page=${page.page}&size=${page.size}&orders=${page.orders}`);
        }
        console.log(url);
        return HttpRequestClient.GET(url);
    }

    modify(condition: ConZoneTypeCondition, module: {id: string, menu: {id: string}}) {
        if (condition.uuidList.length !== 1) {
            Zero3Alert.req('只能选择 1 条要修改的记录！');
        } else {
            HttpRequestClient.router
                .navigate([UrlRou.conZoneType.modify, {moduleId: module.id, menuId: module.menu.id, uuid: condition.uuidList[0]}]);
        }
    }

    update(domain: ConZoneType, callback: {success: (result) => void, error: (result) => void}): Observable<any> {
        const modifyDomain = new ConZoneType(domain);
        ModifyDomain.clear(modifyDomain);
        return HttpRequestClient.PUT(UrlApi.conZoneType.modify, modifyDomain, callback);
    }

    info(condition: ConZoneTypeCondition): Observable<any> {
        const url = `${UrlApi.conZoneType.info}?uuid=${condition.uuid}`;
        return HttpRequestClient.GET(url);
    }

}
